<template>
  <div class="wrapper">
    <!-- showSwiper：computed(监控数据)阶段触发。有数据才会渲染swiper。解决了空数组而渲染了最后一张图片的问题 -->
    <swiper :options="swiperOption" v-if="showSwiper">
      <!-- slides -->
      <swiper-slide v-for="item of list" :key="item.id">
        <img class="swiper-img" :src="item.imgUrl">
      </swiper-slide>
      <!-- Optional controls swiper-slide swiper组件里定义了name=pagination的slot-->
      <div class="swiper-pagination"  slot="pagination"></div>
      <!-- <div class="swiper-button-prev" slot="button-prev"></div> -->
      <!-- <div class="swiper-button-next" slot="button-next"></div> -->
      <!-- <div class="swiper-scrollbar"   slot="scrollbar"></div> -->
    </swiper>
  </div>
</template>

<script>
// 由于swiper是子组件，所以变量必须是个函数，所以有data: function(){ return {xxx} },
// swiper中,绑定了变量swiperOption，因此有swiperOption： xxx
// pagination是配置项，对应是slot="pagination"的样式
export default{
  name: 'HomeSwiper',
  props: {
    list: Array
  },
  data () {
    return {
      swiperOption: {
        pagination: '.swiper-pagination',
        loop: true
      }
    }
  },
  computed: {
    showSwiper () {
      return this.list.length
    }
  }
}
</script>

<style lang='stylus' scoped>
// 宽度100%展开，底部相对宽度，撑开26.67%(图片宽高是750x200,因此高/宽=26.67),而height表示父级元素的高度
// swiper-pagination-bullet-active是组件的样式。不能在当前组件渲染（定义了scoped），所以这里用了穿透 >>>
// overflow: hidden作用 1 溢出隐藏 2 清除浮动 3 解决外边距塌陷
  .wrapper
    width: 100%
    height: 0
    overflow: hidden
    padding-bottom: 26.67%
    background: #eee
    .swiper-pagination-bullet-active {
      background: red !important
    }
    .swiper-pagination-bullets {
      bottom: .4rem
    }
    .swiper-img {
      width: 100%
    }
  .wrapper >>> .swiper-pagination-bullet-active {
      background: #fff
  }
</style>
